package pers.ljf.escloud.controller.sys;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;
import pers.ljf.escloud.annotation.SysLog;
import pers.ljf.escloud.common.dto.SettingDTO;
import pers.ljf.escloud.common.req.EntSaveRequest;
import pers.ljf.escloud.common.utils.BinaryUtils;
import pers.ljf.escloud.dao.entity.app.Enterprise;
import pers.ljf.escloud.dao.entity.app.User;
import pers.ljf.escloud.service.dto.Result;
import pers.ljf.escloud.service.modules.app.BucketService;
import pers.ljf.escloud.service.modules.app.DepartmentService;
import pers.ljf.escloud.service.modules.app.EnterpriseService;
import pers.ljf.escloud.service.modules.app.UserService;
import pers.ljf.escloud.service.modules.sys.SysUserService;
import pers.ljf.escloud.service.util.PageUtils;

import javax.annotation.Resource;
import java.util.Map;

@RestController
@RequestMapping("/sys/ent")
public class SysEntController extends AbstractController{
    @Resource
    private EnterpriseService enterpriseService;

    @Resource
    private DepartmentService departmentService;

    @Resource
    private UserService userService;

    @Resource
    private SysUserService sysUserService;

    @Resource
    private BucketService bucketService;

    @PostMapping("/save")
    @SysLog("新建企业")
    @Transactional
    public Result save(@RequestBody EntSaveRequest form){
        enterpriseService.save(form);
        userService.save(form);
        sysUserService.save(getUserId(),form);
        departmentService.save(form);
        bucketService.save(form);
        return Result.ok();
    }

    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> params) {
        PageUtils page = enterpriseService.queryPage(params);

        return Result.ok().put("page", page);
    }

    /**
     * 删除用户
     */
    @SysLog("删除企业")
    @PostMapping("/delete")
    public Result delete(@RequestBody Long[] entIds){
        enterpriseService.deleteBatch(entIds);
        return Result.ok();
    }

    /**
     * 用户信息
     */
    @GetMapping("/info/{entId}")
    public Result info(@PathVariable("entId") Long userId){
        Enterprise enterprise = enterpriseService.getById(userId);
        User user = userService.getById(enterprise.getAdminId());
        enterprise.setAdminUsername(user.getUsername());
        return Result.ok().put("enterprise", enterprise);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Enterprise enterprise) {
        enterpriseService.updateById(enterprise);
        return Result.ok();
    }

    @PostMapping("/settings/save")
    public Result settingsUpdate(@RequestBody SettingDTO req) {
        Assert.isTrue(req.getEntId() != null, "企业ID不可为空");
        Enterprise enterprise = enterpriseService.getById(req.getEntId());
        Long settings = enterprise.getSettings();
        Long newSettings = BinaryUtils.setBitByIndex(settings, req.getSettingId(), req.getEnabled());
        enterprise.setSettings(newSettings);
        enterpriseService.updateById(enterprise);
        return Result.ok();
    }
}
